package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.PdaCarSendResAdapter;
import com.mflex.WHSmartWhouseAd.model.CommitPdaCarSendResModel;
import com.mflex.WHSmartWhouseAd.model.PdaCadSendingResult;
import com.mflex.WHSmartWhouseAd.model.PdaCarSendResInfo;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//import butterknife.ButterKnife;


public class PDACarReceivingActivity extends BaseActivity implements AdapterView.OnItemClickListener  {
    // 操作人员
    EditText stvOperator;
    // 车号
    EditText stvWagonNumber;
    // 楼层
    EditText stvFloor;
    // 列表
    ListView lvList;
    // 清空
    Button buttonClear;
    // 提交
    Button buttonSubmit;

    // loading
    MiniLoadingDialog miniLoadingDialog;
    List<PdaCarSendResInfo>PdaCarReceivingResModelList=new ArrayList<>();
    CommitPdaCarSendResModel model = new CommitPdaCarSendResModel();
    PdaCadSendingResult input = new PdaCadSendingResult();
    private String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_pda_car_receiving);
//        ButterKnife.bind(this);
        // 弹窗提示
        miniLoadingDialog =WidgetUtils.getMiniLoadingDialog(this,"加载...");
//        id绑定
        stvOperator = findViewById(R.id.stv_Operator);
        stvWagonNumber = findViewById(R.id.stv_WagonNumber);
        stvFloor = findViewById(R.id.stv_Floor);
        lvList = findViewById(R.id.lvList);
        buttonClear = findViewById(R.id.button_clear);
        buttonSubmit = findViewById(R.id.button_submit);
        // 传递数据
        Intent intent = getIntent();
        userName = intent.getStringExtra("User");
        // 视图按键监听器
        stvWagonNumber.setOnKeyListener(this);
        stvOperator.setOnKeyListener(this);
        buttonSubmit.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
        stvOperator.requestFocus();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                PdaCarReceivingResModelList.clear();
                PdaCarSendResAdapter PdaCarSendResAdapter = new PdaCarSendResAdapter(PDACarReceivingActivity.this,PdaCarReceivingResModelList);
                lvList.setAdapter(PdaCarSendResAdapter);
                stvWagonNumber.setText("");
                stvFloor.setText("");
                break;
            case R.id.button_submit:
                saveButtonMd(view);
                break;
            default:
                break;
        }

    }
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_WagonNumber: // 盘号
                     chekPackageNumber();
                    break;
                case R.id.stv_Operator: // 操作人
                    chekOperator();
                    break;
                default:
                    break;
            }
        }
        return false;
    }
    // 扫操作人触发接口
    private void chekOperator() {
        if(StringUtils.isEmpty(stvOperator.getText().toString())) {
            return;
        }
        miniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/CheckActionUser")
                .params("UserName",stvOperator.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .execute(new SimpleCallBack<Boolean>() {
                    @Override
                    public  void  onSuccess(Boolean response) {
                        miniLoadingDialog.dismiss();
                    }
                    @Override
                    public void onError(ApiException e) {
                        miniLoadingDialog.dismiss();
                        stvOperator.setText("");
                        stvOperator.requestFocus();
                        MediaPlayer mp = MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(PDACarReceivingActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }


//    车号回车事件触发
    private void chekPackageNumber() {
        if(StringUtils.isEmpty(stvWagonNumber.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(PDACarReceivingActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("车号未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        // 重复扫描卡控----
        boolean ischeck = true;
        if(!PdaCarReceivingResModelList.isEmpty()) {
            for(PdaCarSendResInfo item: PdaCarReceivingResModelList) {
                if(Objects.equals(item.getCarNumber(), stvWagonNumber.getText().toString())) {
                    ischeck = false;
                    break;
                }
            }
        }
        if(!ischeck) {
            MediaPlayer mp = MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(PDACarReceivingActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("车号"+stvWagonNumber.getText().toString()+"在列表中已存在")
                    .positiveText(R.string.lab_submit)
                    .show();
            stvWagonNumber.setText("");
            stvWagonNumber.requestFocus();
            return;
        }
        // 测试
//        PdaCarSendResInfo items = new PdaCarSendResInfo();
//        items.setCarNumber("车1");
//        items.setTaskNo("20241205");
//        items.setReelQty("100");
//        items.setQty("10");
//        PdaCarReceivingResModelList.add(items);
//        PdaCarSendResAdapter PdaCarSendResAdapter = new PdaCarSendResAdapter(PDACarReceivingActivity.this,PdaCarReceivingResModelList);
//        lvList.setAdapter(PdaCarSendResAdapter);
        // end
        input.setCarNumber(stvWagonNumber.getText().toString());
        input.setFloor(stvFloor.getText().toString());
        input.setSelectType("1");
        miniLoadingDialog.show();
        XHttp.post("api/WHSmartWhousePDA/PdaCarSend")
                .upJson(JsonUtil.toJson(input))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(150000)
                .execute(new SimpleCallBack<CommitPdaCarSendResModel>() {
                    @Override
                    public  void  onSuccess(CommitPdaCarSendResModel response) {
                        miniLoadingDialog.dismiss();
                        PdaCarReceivingResModelList.addAll(response.getPdaCarSendRowDtos());
                        if(StringUtils.isEmpty(stvFloor.getText().toString())) {
                            stvFloor.setText(PdaCarReceivingResModelList.get(0).getFloor());
                        }
                        PdaCarSendResAdapter PdaCarSendResAdapter = new PdaCarSendResAdapter(PDACarReceivingActivity.this,PdaCarReceivingResModelList);
                        lvList.setAdapter(PdaCarSendResAdapter);
                        stvWagonNumber.setText("");
                        stvWagonNumber.requestFocus();
                    }
                    @Override
                    public void onError(ApiException e) {
                        miniLoadingDialog.dismiss();
                        stvWagonNumber.setText("");
                        stvWagonNumber.requestFocus();
                        MediaPlayer mp =MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(PDACarReceivingActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }
//    提交
    private void saveButtonMd(View v) {
        if(PdaCarReceivingResModelList.isEmpty()){
            MediaPlayer mp = MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(PDACarReceivingActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("车号未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        if(StringUtils.isEmpty(stvOperator.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(PDACarReceivingActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("操作人未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        model.setActionUser(stvOperator.getText().toString());
        model.setPdaCarSendRowDtos(PdaCarReceivingResModelList);
//        CommonRequest request =new CommonRequest();
//        request.setJson(JsonUtil.toJson(model));
        String json = JsonUtil.toJson(model);
        Log.d("1234",json);
        miniLoadingDialog.show();
        XHttp.post("api/WHSmartWhousePDA/PadCarCollect")
                .upJson(JsonUtil.toJson(model))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        miniLoadingDialog.dismiss();
                        XToastUtils.success("提交成功",3000);
                        PdaCarReceivingResModelList.clear();
                        PdaCarSendResAdapter PdaCarSendResAdapter = new PdaCarSendResAdapter(PDACarReceivingActivity.this,PdaCarReceivingResModelList);
                        lvList.setAdapter(PdaCarSendResAdapter);
                        stvWagonNumber.setText("");
                    }
                    @Override
                    public void onError(ApiException e) {
                        miniLoadingDialog.dismiss();
                        MediaPlayer mp =MediaPlayer.create(PDACarReceivingActivity.this,R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(PDACarReceivingActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }

                });
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}